import com.interactiveAlchemy.utils.Debug;
import utils.Delegate;
import data.Value;
import functionnodes.FunctionNodeAdd;
/**
 * @author loaner
 */
 
 //TODO: create a withc functionnode? so that we can invoke methods on clips?
 
 //TODO: create a new functionnode -- a special function? no, not really....
 
 //TODO: create a macro functionnode and figure out how to test it.
 // HINT: it will require <q> and <uq> to be working properly...
 // ... I expect to have to search recursively for <uq> nodes and to replace them
 // by evaluating them IN THE DYNAMIC SCOPE.
 // that's the big difference between macros and functions!
 // functions have their own scope, and it's created lexically -- 
 // at the time that the function is defined!
 // the only way to touch it from outside is via arguments.
 //
 // and it's those arguments that are evaluated -- converted from XML to values, in the dynamic scope -- 
 // before the function gets to see them.
 //
 // now, the function does have its own scope, and it keeps that scope going,
 // so it can retain values -- what C would call static vars.
 // it's like a little island with its own ecosystem.
 // 
 // the only way a function can touch its context is via global vars
 // or via return values.
 // 
 // but macros are different in 2 ways:
 // 1) arguments aren't evaluated at all, in any scope, before being passed; and
 // 2) the things that get evaluated in the macro body are ALWAYS evaluated ONLY in the dynamic scope.
 
class Main {

	private var x:XML;
	
	
	public static function main()
	{
		trace("_root instanceof MovieClip = " + (_root instanceof MovieClip ));
		_root.lineStyle( 1, 0xff0000, 100);
		_root.moveTo(0,0);
		_root.lineTo(200,200);
		_root.createTextField( "inputfield", 1, 10, 10, 200, 200 );
		var inputfield:TextField = _root.inputfield;
		inputfield.type = "input";
		inputfield.background = true;
		inputfield.backgroundColor = 0xffffff;
		inputfield.border = true;
		inputfield.borderColor = 0x000000;
		
		inputfield.text = "Howdy";
		
		MovieClip.prototype.onEnterFrame = function() 
		{ 
			if( this.onEnterFrameHandler != undefined )
			{
				trace("at least onEnterFrameHandler is something... it's " + this.onEnterFrameHandler);
				this.onEnterFrameHandler.reduceValues( this, null );
//				Evaluator.convertXMLtoValue( this.onEnterFrameXML, this.scope ); 
			}
		};
		var m:Main = new Main();
		
	}
	
/*
 * A cool but irrelevant sample showing how to use
 * AS2Lib's Distributors
 * for stronger and easier event usage
 * 
 * Note that IOEvents is an Interface,
 * so our Main and SomeListener classes would need to implement it....
 * 
 * public function Main()
				{
					var myControl:EventDistributorControl = new SimpleEventDistributorControl( IOEvents );
					trace("myControl = " + myControl );
					var sl:SomeListener = new SomeListener();
					myControl.addListener( this );
					myControl.addListener( sl );
					var distributor:IOEvents = myControl.getDistributor();
					trace("distributor = " + distributor );
					distributor.onSuccess( "this works");
				}
 */
	
	public static function myTrace( msg:String, classname:String, filename:String, linenumber:String)
	{
		Debug.write( msg + ", " + classname + "\n");	
	}
	
	public function Main()
	{
		trace("Making a Main, dammit");
		x = new XML();
		x.ignoreWhite = true;
		x.load("flisp.xml");
		x.onLoad = Delegate.create( this, onXMLLoaded );
	}
	
	public function onXMLLoaded()
	{
		trace("Loaded XML " + x);	
		
		Evaluator.init( _root );
		trace( Evaluator.convertXMLtoValue( x.firstChild, _root ) );
	}
	
}